package com.example.teststarter.service;

import com.example.teststarter.interfaceService.MyFunction;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Service
public class FunctionService {
    public static Map<String, MyFunction<Object, String>> map = new HashMap<>();

    @PostConstruct
    public void init() {
        map.put("A", FunctionService::fun01);
        map.put("B", FunctionService::fun02);
        map.put("C", FunctionService::fun03);
    }

    private static String fun03(Object... o) {
        System.out.println("fun03=====>");
        for (Object o1 : o) {
            System.out.println(o1);
        }
        return "CCC";
    }

    private static String fun02(Object... o) {
        System.out.println("fun02=====>");
        for (Object o1 : o) {
            System.out.println(o1);
        }
        return "BBB";
    }

    private static String fun01(Object... o) {
        System.out.println("fun01=====>");
        for (Object o1 : o) {
            System.out.println(o1);
        }
        return "AAA";
    }

    public String getFunctionResult(String type, String value, Integer num) {
        // 以前的if-else
        /*
        if(type.equals("A")){
            fun01("a", 1);
        }else if(type.equals("B")){
            fun02("b", 2);
        }else if(type.equals("C")){
            fun03("c", 3);
        }
        */
        // 优化的if-else
        return map.get(type).apply(value, num);
    }
}
